package com.seecen.controller;

import com.seecen.pojo.Recipes;
import com.seecen.service.RecipesService;
import com.seecen.utils.JwtHelper;
import com.seecen.utils.Result;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.ContentCachingRequestWrapper;

import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author W
 * @description 菜谱相关接口
 * @createDate 2025-07-21 15:12:04
 */
@RestController
@RequestMapping("/receipes")
@CrossOrigin
public class RecipesController {

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private RecipesService recipesService;

    // 图片上传接口
    @PostMapping("/postImg")
    public Result<String> postImg(@RequestParam("image") MultipartFile file) {
        System.out.println("开始上传图片");
        System.out.println("接收到的文件对象: " + file);
        System.out.println("文件名: " + file.getOriginalFilename());
        System.out.println("文件大小: " + file.getSize());
        System.out.println("文件类型: " + file.getContentType());
        
        if (file == null || file.isEmpty()) {
            System.out.println("文件为空");
            return Result.build(null, 400, "上传的文件为空");
        }

        try {
            String imageUrl = recipesService.saveImage(file);
            System.out.println("图片上传成功" + imageUrl);
            Result<String> result = Result.ok(imageUrl).message("图片上传成功");
            System.out.println("返回结果: " + result);
            return result;
        } catch (Exception e) {
            System.out.println("图片上传失败");
            e.printStackTrace();
            return Result.build(null, 500, "图片上传失败：" + e.getMessage());
        }

    }
    
    @PostMapping("/recipesById")
    public Result recipesById(@RequestParam("id") Integer id){
        Result ret = recipesService.recipesById(id);
        return ret;
    }
    
    // 添加菜谱接口
    @PostMapping("/addRecipe")
    public Result<String> addRecipe(@RequestBody Recipes recipe, @RequestParam(required = false) Integer categoryId, HttpServletRequest request) {
        // 添加详细的字段检查日志
        if (recipe != null) {
            System.out.println("菜谱对象字段详情:");
            System.out.println("- recipesName: " + recipe.getRecipesName() + " (类型: " + (recipe.getRecipesName() != null ? recipe.getRecipesName().getClass() : "null") + ")");
            System.out.println("- recipesRank: " + recipe.getRecipesRank() + " (类型: int)");
            System.out.println("- recipesUrl: " + recipe.getRecipesUrl() + " (类型: " + (recipe.getRecipesUrl() != null ? recipe.getRecipesUrl().getClass() : "null") + ")");
            System.out.println("- description: " + recipe.getDescription() + " (类型: " + (recipe.getDescription() != null ? recipe.getDescription().getClass() : "null") + ")");
            System.out.println("- foodMaterials: " + recipe.getFoodMaterials() + " (类型: " + (recipe.getFoodMaterials() != null ? recipe.getFoodMaterials().getClass() : "null") + ")");
            System.out.println("- createTime: " + recipe.getCreateTime() + " (类型: " + (recipe.getCreateTime() != null ? recipe.getCreateTime().getClass() : "null") + ")");
            System.out.println("- updateTime: " + recipe.getUpdateTime() + " (类型: " + (recipe.getUpdateTime() != null ? recipe.getUpdateTime().getClass() : "null") + ")");
        }

        // 检查具体哪个字段导致了问题
        if (recipe != null && recipe.getRecipesUrl() != null && !(recipe.getRecipesUrl() instanceof String)) {
            System.out.println("错误：recipesUrl字段不是String类型，实际类型为: " + recipe.getRecipesUrl().getClass());
        }

        // 参数校验
        if (StringUtils.isEmpty(recipe.getRecipesName())) {
            System.out.println("菜谱名称为空，返回错误");
            return Result.build(null, 400, "菜谱名称不能为空");
        }
    
        if (categoryId == null) {
            System.out.println("分类ID为空，返回错误");
            return Result.build(null, 400, "分类ID不能为空");
        }
    
        if (StringUtils.isEmpty(recipe.getRecipesUrl())) {
            System.out.println("封面图片URL为空，返回错误");
            return Result.build(null, 400, "封面图片URL不能为空");
        }

        try {
            // 调用服务层保存菜谱数据及分类关联
            recipesService.saveRecipeWithCategory(recipe, categoryId);
            System.out.println("菜谱保存成功");
            return Result.ok("菜谱保存成功").message("菜谱保存成功");
        } catch (Exception e) {
            System.out.println("菜谱保存失败");
            e.printStackTrace();
            return Result.build(null, 500, "菜谱保存失败：" + e.getMessage());
        }
    }
}